<extend name="Layout:layout_list" />
<block name="body_before_block">
    <script src="__THEME__Plugin/jquery-gridtimer-1.0/jquery-gridtimer-1.0.js" type="text/javascript"></script>
</block>
<block name="body_content_block">
    
    <if condition="$is_empty">
        <div style="color:#F00">当前没有任何外呼策略，请创建一个新策略! <a href="{:AddonU('TaskStrategy/item_edit')}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true">新增</a>(创建成功后请手动刷新页面!)</div>
        <else />
        <div id="cc" class="easyui-layout" style="width:100%;min-height:480px;">
            <div data-options="region:'west',split:true" title="全部策略" style="width:155px; max-height:480px;">
                <ul class="sotab">
                    <foreach name="list" item="vo">
                        <li id="sotab-{$vo.id}" class="notbreakline" title="{$vo.name}"><a href="{:AddonU('TaskStrategy/index','tsid='.$vo['id'])}">{$vo.name|sofn_subtext=###,9,'..'}</a></li>
                    </foreach>
                </ul>
            </div>
            <div data-options="region:'center'">
                <div id="tb" style="padding:2px 5px;">
                    <div style=" display: table; width:100%; overflow:hidden;">
                        <div style="float: left">
                            <table cellpadding="0" cellspacing="0">
                                <tr>
                                    <td>
                                        <span id="blance-tip" style="color:#F00; font-size: 14px; font-weight:bold; background-color:#FC0; padding:2px; display: none;" data-url="{:AddonU('Task/check_user_blance_tip','siteID='.$data['siteID'])}"></span>
                                        <a href="{:AddonU('Task/add_number','tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true">添加号码</a>
                                        <a href="{:AddonU('Task/generate_numberq','tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true">快速生成号码</a>
                                        <!--<a href="{:AddonU('Task/generate_numberb','tsid='.$id)}" class="button btn-icon lnk-add" data-dialog="{width:400}">批量生成号码</a>-->
                                        <a href="{:AddonU('Task/generate_number','tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true">生成号码</a>
                                        <a href="{:AddonU('Task/import_number','tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true" data-tag="drhm">导入号码</a>
                                <if condition="equal_permission_name('批量删除任务',$loginData['role_id'])">
                                    <a href="{:AddonU('Task/task_delete','id=varkey&tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_remove lnk_remove" iconCls="icon-remove" plain="true" data-success-fn="delete_task_fn(data)">删除</a>
                                </if>
                                <if condition="equal_permission_name('回收任务',$loginData['role_id'])">
                                    <a class="easyui-linkbutton lnk_grid_info" id="lnk_sjwh" href="{:AddonU('Task/task_recycling','id=varid&tsid='.$tsid)}" data-need-checking="true" iconCls="icon-recycle" plain="true" data-tag="sjwh">数据维护</a>
                                </if>
                                <!--
                                <a href="{:AddonU('Task/set_task_state','state=1&id=varid&tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_remove" iconCls="icon-start" plain="true" data-confirm-message="您确定要启动任务吗">单个启用</a>
                                <a href="{:AddonU('Task/set_task_state','state=0&id=varid&tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_remove" iconCls="icon-stop" plain="true" data-confirm-message="您确定要停止任务吗">单个停止</a>
                                -->
                                <a href="{:AddonU('Task/set_task_state_batch','state=1&tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_process" iconCls="icon-start" plain="true">全部启用</a>
                                <a href="{:AddonU('Task/set_task_state_batch','state=0&tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_process" iconCls="icon-stop" plain="true">全部停止</a>
                                <a href="{:AddonU('TaskStrategy/info','tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_info" iconCls="icon-info" plain="true">策略详情</a>
                                <a href="{:AddonU('QueueStatus/queue_status_bystrategy', 'tsid='.$tsid)}" class="easyui-linkbutton lnk_grid_info" iconCls="icon-monitor" plain="true">队列监控</a>
                                </td>
                                <td>
                                    {$speedcontrol}</td>
                                <td style="padding-left:5px;">
                                    <input type="text" id="grid-timer" value="0" /></td>
                                </tr>
                            </table>
                        </div>
                        <div style="float: right">
                            <span>策略操作：</span>
                            <if condition="equal_permission_name('新增外呼模式',$loginData['role_id'])"><a href="{:AddonU('TaskStrategy/item_edit')}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-add" plain="true">新增</a></if>
                            <if condition="equal_permission_name('编辑外呼模式',$loginData['role_id'])"><a href="{:AddonU('TaskStrategy/item_edit','id='.$tsid)}" class="easyui-linkbutton lnk_grid_add" iconCls="icon-edit" plain="true">编辑</a></if>
                            <if condition="equal_permission_name('删除外呼模式',$loginData['role_id'])"><a href="{:AddonU('TaskStrategy/item_delete','id='.$tsid)}" class="easyui-linkbutton lnk_grid_process" iconCls="icon-remove" plain="true" data-success-fn="reload_page()">删除</a></if>
                        </div>
                        <div style="clear: both; "></div>
                    </div>
                </div>
                <table id="main_grid" class="easyui-datagrid" data-url="{:AddonU('Task/get_task_list_json')}" data-query-params='"tsid":"{$tsid}"' title="{$data.name}"></table>
            </div>
        </div>
        <div id="info-dialog" title="策略详情" style="display: none; max-height:210px; overflow: auto; width:660px;word-wrap: break-word;word-break: normal;">{$summary}</div>    
    </if>
    <input type="hidden" id="task_state_switch_url" value="{:AddonU('Task/set_task_state','state=varstate&id=varid&tsid='.$tsid)}" />
</block>
<block name="body_after_block">
    <script type="text/javascript">
        // 页面初始化
        var curd_grid = CurdGrid.newInstance();
        curd_grid.config({
            toolbar: '#tb',
            fitColumns: true,
            showFooter: true,
            columns: [[
                    {field: 'ck', checkbox: true},
                    {title: "任务名称", field: "name", sortable: true, formatter: function (value, row) {
                            return '<span title="taskID=' + row.id + ',strategyID=' + row.taskStrategyID + '">' + value + '</span>';
                        }},
                    {title: "号码总数", field: "telsCount", sortable: true},
                    {title: "已呼数量", field: "callCount", sortable: true},
                    {title: "未呼数量", field: "unCallCount", sortable: true},
                    {title: "完成量", field: "callRateText", sortable: true, formatter: function (value, row) {
                            if (!value) {
                                return '';
                            }
                            return '<div class="bor"><div class="bg" style="width:' + (value > 100 ? 100 : value) + '%">' + value + '%</div></div>';
                        },
                        styler: function () {
                            return {class: 'td-progressbar'};
                        }},
                    {title: "应答数量", field: "callSuccessCount", sortable: true},
                    {title: "失败数量", field: "callFailCount", sortable: true},
                    {title: "未接数量", field: "noAnswerCount", hidden: true},
                    {title: "应答率", field: "connRateText", sortable: true, formatter: function (value, row) {
                            if (!value) {
                                return '';
                            }
                            return '<div class="bor"><div class="bg" style="width:' + (value > 100 ? 100 : value) + '%">' + value + '%</div></div>';
                        },
                        styler: function () {
                            return {class: 'td-progressbar'};
                        }},
                    {title: "到座席量", field: "arriveSeatCount", sortable: true},
                    {title: "到座席率", field: "arriveSeatRateText", sortable: true, formatter: function (value, row) {
                            if (!value) {
                                return '';
                            }
                            return '<div class="bor"><div class="bg" style="width:' + (value > 100 ? 100 : value) + '%">' + value + '%</div></div>';
                        },
                        styler: function () {
                            return {class: 'td-progressbar'};
                        }},
                    {title: "创建日期", field: "createdTimeText", sortable: true},
                    {title: "任务状态", field: "stateText", sortable: true, styler: function (value, row) {
                            if (value == '停止') {
                                return 'color:#F00';
                            } else {
                                return 'color:#090';
                            }
                        }},
                    {title: "操作", field: "state", sortable: true, formatter: function (value, row) {
                            if (undefined == value) {
                                return;
                            }
                            if (value > 0) {
                                return '停止';
                            } else {
                                return '启动';
                            }
                        }, styler: function (value, row) {
                            return 'cursor: pointer;'
                        }},
                    {title: "数据维护", field: "id_sjwh", formatter: function (value, row) {
                            if (row.id) {
                                return '数据维护';
                            }
                        }, styler: function (value, row) {
                            return 'cursor: pointer;'
                        }},
                    {title: "操作", field: "id", formatter: function (value, row) {
                            if(!value){
                                return;
                            }
                            var btns = ['.lnk_remove', '#lnk_sjwh'];
                            return curd_grid.set_operatecell_buttons(btns, function (sbtn) {
                                return sbtn.replace('varid', value).replace('varkey', value).replace('{userName}', row.userName);
                            });
                        }},
                ]],
            onClickCell: function (index, field, value) {
                if ('state' != field && 'id_sjwh' != field) {
                    return;
                }
                var rows = $('#main_grid').datagrid('getRows');
                if (!rows) {
                    return;
                }
                for (var i in rows) {
                    if (i == index) {
                        switch (field) {
                            case 'state':
                                lnk_state_switch_handler(rows[i].id, rows[i].state);
                                break;
                            case 'id_sjwh':
                                lnk_sjwh_handler(rows[i].id);
                                break;
                        }
                        break;
                    }
                }
            },
            onLoadSuccess: function () {
                var $tr = $('.datagrid-header-row');
                $tr.find('td[field="telsCount"] span').attr('title', 'S-telsCount');
                $tr.find('td[field="callCount"] span').attr('title', 'K-callCount');
                $tr.find('td[field="unCallCount"] span').attr('title', 'K-unCallCount');
                $tr.find('td[field="callRateText"] span').attr('title', 'K-callRateText');
                $tr.find('td[field="callSuccessCount"] span').attr('title', 'K-callSuccessCount');
                $tr.find('td[field="callFailCount"] span').attr('title', 'K-callFailCount');
                $tr.find('td[field="connRateText"] span').attr('title', 'K-connRateText');
                $tr.find('td[field="arriveSeatCount"] span').attr('title', 'S-arriveSeatCount');
                $tr.find('td[field="arriveSeatRateText"] span').attr('title', 'S-arriveSeatRateText');
            },
        });
        curd_grid.set_edit_dialog_config = function (options, ifrm_id) {
            if ('sjwh' == options.tag) {
                for (var i in options.buttons) {
                    if (options.buttons[i].text == '保存') {
                        options.buttons[i].text = '导出到新任务';
                    }
                }
                options.buttons.unshift({
                    text: '导出到Excel',
                    iconCls: 'icon-save',
                    handler: function () {
                        $('#' + ifrm_id).contents().find("form")[0].submit();
                    }
                });
            } else if ('drhm' == options.tag) {
                for (var i in options.buttons) {
                    if (options.buttons[i].text == '保存') {
                        options.buttons[i].text = '立即导入';
                    }
                }
            }
        }
        curd_grid.init();
        $(document).ready(function () {
            $('#grid-timer').gridTimer({elapsed: function () {
                    $('#main_grid').datagrid('reload');
                }});
        })
        // 更改呼叫速度 2014-10-6 by sutroon
        $('#speed').change(function () {
            var val = $(this).val();
            if (!confirm('您确定要更改呼叫速度吗?')) {
                return;
            }
            var url = $(this).data('updateUrl');
            if (undefined == url) {
                alert('URL错误!');
                return;
            }
            $.get(url.replace('varspeed', val), function (data) {
                if (data.status) {
                    $.messager.alert('提示', data.info);
                } else {
                    alert(data.info);
                }
            })
        })
        function delete_task_fn(data) {
            alert(data.info);
        }
        function reload_page() {
            location.reload();
        }
        function lnk_state_switch_handler(id, state) {
            var url = $('#task_state_switch_url').val();
            url = url.replace('varstate', state > 0 ? 0 : 1).replace('varid', id);
            $.get(url, function (data) {
                if (!data.status) {
                    alert(data.info);
                }
                curd_grid.reload_grid();
            });

        }
        function lnk_sjwh_handler(id) {
            var url = $('#lnk_sjwh').attr('href');
            curd_grid.open_iframe_dialog({tag: 'sjwh', title: '数据维护', icon: 'icon-recycle', url: url.replace('varid', id)});
        }
    </script>
</block>
